package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
//@Transactional
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IRegionService regionService;



 
    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public Page<Destination> listByQuery(DestinationQuery qo) {
        // 创建查询对象
        Query query = new Query();
        if (qo.getKeyword() != null){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if (qo.getParentId()!= null){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }
        // 分页封装参数对象
        Pageable  pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        // 传进查询对象
        return DBHelper.listByQuery(mongoTemplate,pageable,query,Destination.class,qo);
    }


    @Override
    public List<Destination> queryByRegionId(String rid) {
        List<Destination> destinations = new ArrayList<>();
        // 如果 rid 是-1,则表示默认查中国
        if ("-1".equals(rid)){
            destinations = destinationRepository.findByParentName("中国");
        }else {
            // 查询目的地对应的区域信息
            Region region = regionService.get(rid);
            // 从 region 对象中获取相关的目的地 id
            List<String> refIds = region.getRefIds();
            destinations = destinationRepository.findByIdIn(refIds);
        }

        // 遍历 destinations 集合,根据 parentId 查出每个集合下的目的地的目的地
        for (Destination destination : destinations) {
            // 按照体验度, 每个目的地下的小目的地只显示 5 个
            Pageable pageable = PageRequest.of(0,5);
            List<Destination> list = destinationRepository.findByParentId(destination.getParentId(),pageable);
            destination.setChildren(list);
        }
        return destinations;
    }


    @Override
    public List<Destination> queryByDestId(String destId) {
        List<Destination> destinations = new ArrayList<>();
        this.createToasts(destinations,destId);
        Collections.reverse(destinations);
        return destinations;
    }

    /**
     * 传入一个 list ,如果有目的地 id ,则添加到 list 中
     * @param destinations
     * @param destId
     */
    public void createToasts(List<Destination> destinations,String destId) {
      if (!StringUtils.hasLength(destId)){
          return;
      }
      Destination destination = this.get(destId);
      destinations.add(destination);
      if (StringUtils.hasLength(destination.getParentId())){
          this.createToasts(destinations,destination.getParentId());
      }
    }
}
